﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using University.Workshop.DataAccess;
using University.Workshop.Business;
using University.Workshop.Model;
using System.Globalization;
using System.Threading;
using University.Workshop.DataAccess.LanguagePack.ClientLanguagePack;

namespace University.Workshop.ClientApp
{
    /// <summary>
    /// Interaction logic for Profile.xaml
    /// </summary>
    public partial class Profile : Window
    {
        User user = new User();
        User userRequest=new User();
        RelationshipsManager relations=new RelationshipsManager();
        Client client = new Client();
        DatabaseQuery query = new DatabaseQuery();
        string language;
        public Profile(string languageReceived)
        {
            InitializeComponent();
            language=languageReceived;
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
            ApplyLanguage();
            AddFriend.IsEnabled = false;
            AddFriend.Visibility = Visibility.Hidden;
            labelMessage.Visibility = Visibility.Hidden;
        }

        private void ApplyLanguage()
        {
            label4.Content = ClientLanguagePack.ProfileLabelHands;
            label5.Content = ClientLanguagePack.ProfileLabelBestHand;
            AddFriend.Content = ClientLanguagePack.ProfileButton;
            label1.Content = ClientLanguagePack.ProfileLabelUser;
            label6.Content = ClientLanguagePack.ProfileLabelLevel;
            label2.Content = ClientLanguagePack.ProfileLabelEmail;
            label3.Content = ClientLanguagePack.ProfileLabelChips;
            label5.Content = ClientLanguagePack.ProfileLabelStatus;
            label7.Content = ClientLanguagePack.ProfileLabelBestHand;
        }
        public Profile(User user, User userRequest, RelationshipsManager relations)
        {
            InitializeComponent();
            this.relations = relations;
            this.user=user;
            this.userRequest=userRequest;
            labelUser.Content = userRequest.Name;
            labelMail.Content = userRequest.Email;
            labelLevel.Content = Convert.ToString(userRequest.UserLevel, CultureInfo.InvariantCulture) + " " + userRequest.GetLevel;
            labelChips.Content = Convert.ToString(userRequest.Chips,CultureInfo.InvariantCulture);
            labelTimeWon.Content = Convert.ToString(userRequest.TimeWon, CultureInfo.InvariantCulture);
            long amount = client.BestHand(userRequest.Name);
            if(amount==-1)
                labelBestHand.Content = "This user have never played";
            else
                labelBestHand.Content = Convert.ToString(amount + " chips");
            if (userRequest.Status == 1)
                labelStatus.Content = "Connected";
            else
                labelStatus.Content = "Disconnected";
            if (IsRealated())
                AddFriend.IsEnabled = false;
            if (userRequest.Name == user.Name)
            {
                labelMessage.Content = "You are this user";
                AddFriend.IsEnabled = false;
            }
        }

        private bool IsRealated()
        {
            foreach (User userOnly in relations.PendingSend)
            {
                if (userRequest.Name == userOnly.Name)
                {
                    labelMessage.Content = "Request sent";
                    return true;
                }
            }
            foreach (User userOnly in relations.PendingReceived)
            {
                if (userRequest.Name == userOnly.Name)
                {
                    labelMessage.Content = "He is waiting you to accept his request";
                    return true;
                }
            }
            foreach (User userOnly in relations.Friends)
            {
                if (userRequest.Name == userOnly.Name)
                {
                    labelMessage.Content = "You are friends";
                    return true;
                }
            }
            return false;
        }

        private bool Add()
        {
            if (client.SendRequest(user.Name, userRequest.Name))
            {
                labelMessage.Content = "Request succefuly sent";
                relations.PendingSend.Add(userRequest);
                relations.PeopleNotKnow.Remove(userRequest);
                return true;
            }
            else
            {
                labelMessage.Content = "An error ocurred";
                return false;
            }
        }

        private void AddFriend_Click(object sender, RoutedEventArgs e)
        {
            Add();
        }
       
    }
}
